<template>
  <div class="app-container">
    <basic-query-form ref="queryForm" @doQuery="getList" @resetQuery="getList" :dept-type="2" :deptClear="false">
      <template v-slot:queryItem="data">
        <el-form-item label="职位" >
          <el-select v-model="data.queryParams.postCode" placeholder="请选择" clearable size="small">
            <el-option  v-for="dict in positionOptions"
                        :label="dict.name"
                        :key="dict.value"
                        :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </template>
      </basic-query-form>

    <el-table :data="noAssessItemProgrammes">
      <el-table-column type="expand" />
      <el-table-column label="不存在考核对象的方案" prop="programmeName" >
      </el-table-column>
    </el-table>


    <el-table :data="stateList" >
      <el-table-column type="expand" >
        <template slot-scope="scope1">
          <el-table :data="scope1.row.codeVos" size="mini">
            <el-table-column width="150px" label="考核码" style="font-weight: bold" prop="assessmentCode" />
            <el-table-column width="200px" label="用户" prop="showName" />
            <el-table-column label="未完成方案" prop="notCompletedProgramme" :formatter="notCompletedProgrammeFormatter"  />
          </el-table>
        </template>
      </el-table-column>

      <el-table-column label="按岗位统计考核进度" prop="postCode" >
        <template slot-scope="scope">
          <span style="font-weight: bold;font-size: large;">{{postFormat(scope.row, scope.column)}} （{{scope.row.codeCount}}人）</span>
           <el-tag size="medium" :type="scope.row.programmeStatus.type">{{ scope.row.programmeStatus.name}}</el-tag>
        </template>
      </el-table-column>


    </el-table>


  </div>
</template>

<script>
  import BasicQueryForm from '../../components/BasicQueryForm'
  import { stateProgress} from "@/api/kh/progress";
  import { optionsPost } from  "@/api/system/post";
  import request from '@/utils/request';

export default {
  name: "Progress",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 考核进度表格数据
      stateList: [],
      noAssessItemProgrammes:[],
      //考核列表
      AssessmentOptions:[],
      //考核列表
      ProgrammeOptions:[],
      //被考核部门
      deptOptions:[],
      progressStatus:[],
      //职位字典集合
      positionOptions: [],
      yesOrNo:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  components: {
    'BasicQueryForm' : BasicQueryForm
  },
  created() {
    optionsPost().then(response => {
      this.positionOptions = response.data;
    });
    this.getDicts("kh_progress_status").then(response => {
      this.progressStatus = response.data;
    });

    this.getDicts("sys_yes_no").then(response => {
      this.yesOrNo = response.data;
    });

  },

  methods: {
    getListByQueryForm(){
      this.$refs.queryForm.pageQuery(this.queryParams)
    },
    getCurAssess(row){
      return this.AssessmentOptions.find(item => item.value === row.assessmentId);
    },
    deptFormat(row,column){
      return this.getCurAssess(row).depts.find(item => item.value === row.deptId).name;
    },
    assessFormat(row,column){
      return this.getCurAssess(row).name;
    },
    programmeFormat(row,column){
      return this.getCurAssess(row).programmes.find(item => item.value === row.programmeId).name;
    },
    notCompletedProgrammeFormatter(row,column){
      return row.notCompletedProgramme === undefined ? "" : row.notCompletedProgramme.join(" | ");
    },
    // 字典状态字典翻译
    postFormat(row, column) {
      return this.selectOptionName(this.positionOptions, row.postCode);
    },
    dictDataFormat1(row, column) {
      return this.selectDictLabel(this.yesOrNo, row.editable);
    },
    /** 查询考核进度列表 */
    getList(queryParams) {
      this.queryParams.pageNum = queryParams.pageNum;
      this.queryParams.pageSize = queryParams.pageSize;
      this.loading = true;
      stateProgress(queryParams).then(response => {
        this.stateList = response.data.stateByPostCodeAndAnswerStatus;
        this.noAssessItemProgrammes = response.data.noAssessItemProgrammes;
        this.loading = false;
        this.AssessmentOptions = this.$refs.queryForm.AssessmentOptions;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: undefined,
        assessmentId: undefined,
        programmeId: undefined,
        assessmentCodeId: undefined,
        indexCount: undefined,
        answerStatus: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考核进度";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProgress(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考核进度";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateProgress(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getListByQueryForm();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addProgress(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getListByQueryForm();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除考核进度编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProgress(ids);
        }).then(() => {
          this.getListByQueryForm();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有考核进度数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProgress(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    setAnswerEditable(row) {
      this.$confirm('是否要使该考核结果可再次提交?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return request({
          url: '/kh/progress/setAnswerEditable',
          method: 'post',
          data: row
        })
      }).then(response => {
        if (response.code === 200) {
          this.msgSuccess("修改成功");
          this.getListByQueryForm();
        } else {
          this.msgError(response.msg);
        }
      }).catch(function() {});
    }
  }
};
</script>
